<%@ page language="java" contentType="text/html; charset=UTF-8"
	pageEncoding="UTF-8"%>
<!DOCTYPE html >
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
<title>Insert title here</title>


<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<link rel="stylesheet"
	href="//code.jquery.com/ui/1.11.0/themes/smoothness/jquery-ui.css">
<link rel="stylesheet" href="resources/css/patient/reservation.css">
<script src="//code.jquery.com/jquery-1.10.2.js"></script>
<script src="//code.jquery.com/ui/1.11.0/jquery-ui.js"></script>
<script>

		var name="";
		
		
		$("#tabs").tabs();


	
		$(document).ready(function(){
			$("#select_D").focus();
			$("#select_D").bind("change", fn_selectChange);	 
			$("#sel_doc").bind("change", fn_selectcal);
		});   			
		var doctor_id;

		var idx="";
		var thisday="";	
		var bohum_code;
		var res_time;
		var idx;
		var id;
		var is_holiday;
		var is_ward;
		var is_night;
		var jupsoo_code_id;
		$(document).ready(function(){
			
			$(document).on("click",".nameTr2",function(){
				//alert(is_night+"/"+is_holiday);
				$("#check2").empty();
				idx = $(".nameTr2").index(this);
				//alert(idx);
				bohum_code=$("#bohum").val();
				//alert(bohum_code);
				//alert(doctor_id);
				id = $("input[name='schedule_id']").eq(idx).val();
				res_time = $("input[name='res_time']").eq(idx).val();
				//alert(res_time);
				var reser = "";
				reser+="<table id='sidetable1'>";
				reser+="<tr><th>의사명</th><th>예약날짜</th><th>예약시간</th></tr>";
				reser+="<tr><td>"+name+"</td><td>"+thisday+"</td><td>"+res_time+"</td></tr>";
				reser+="<tr><td colspan='3'><button id='reser' onclick='add();'>예약하기</button></td></tr>";
				reser+="</table>";
				$("#check2").html(reser);
			});
			
		});
		function fn_selectcal(){
			//alert($("#sel_doc").val());
			doctor_id = $("#sel_doc").val();
			//alert(doctor_id);
			$.ajax({
				url: 'showschedule.do',
				data: 'doctor_id='+doctor_id,
				async:false,
				success: function(data2) {
					//alert(data2.enableDay);
					 enableDay=[];
					enableDay = data2.enableDay;

					function selectableDays(date) {
						m = date.getMonth() + 1;
						if(m<10){
							m = "0"+m;
						}
						d = date.getDate();
						if(d<10){
							d = "0"+d;
						}
						dummy = d + "-" +m+ "-" + date.getFullYear(); 
						if ($.inArray(dummy, enableDay) > -1) {
						return [true, ""];
						}
						return [false, ""];
					}
					$(function() { 
						$("#datepicker").datepicker(
						{
							showMonthAfterYear : true,
							minDate : '-0d',
							yearSuffix : '년',
							monthNames : [ '1월', '2월', '3월', '4월', '5월', '6월', '7월',
									'8월', '9월', '10월', '11월', '12월' ],
							monthNamesShort : [ '1월', '2월', '3월', '4월', '5월', '6월',
									'7월', '8월', '9월', '10월', '11월', '12월' ],
							dayNamesMin : [ '일', '월', '화', '수', '목', '금', '토' ],
							dateFormat : 'yy-mm-dd',
							beforeShowDay: selectableDays,
							onSelect : function(dateText, datePicker) {
								$("#currentDate").val(dateText);
								 thisday = dateText;
								
								$.ajax({
									url: 'selectschedule.do',
									data: 'sdate='+thisday+'&doctor_id='+doctor_id,
									dataType : "json",
									success: function(data) {
											$("#sidetable tr").detach();
											 name= data.timelist[0].name;
										
										for (i = 0; i < data.timelist.length; i++) {
											var html="";
											html+="<tr class='nameTr2' name='nameTr'><td><input id='schedule_id' name='schedule_id' value='"+data.timelist[i].schedule_id+"' readonly='readonly'></td>";
											html+="<td><input id='res_time' name='res_time' value='"+data.timelist[i].res_time+"' readonly='readonly'></td></tr>";
											
											$("#sidetable").append(html);
											
										}
								  	}
								});
							}
						});
					});
					for(var i =0; i<schedulelist.length;i++){
						var $s =data2.schedulelist[i].res_time;
					}
			  	}
			});
		}
		 function add(){
				var reser2="";

			 	is_night=$("#is_ward").val();
				is_holiday = $("#is_holiday").val();
				jupsoo_code_id = $("#jupsoo_code_id").val();
				var is_night=$("#is_night").val();
				var is_holiday = $("#is_holiday").val();
				var jupsoo_code_id = $("#jupsoo_code_id").val();

				reser2+="<form id='addsubmit' action='addreser.do'>";
				reser2+="<input type='hidden' name='bohum_code_id' value='"+bohum_code+"'>";
				reser2+="<input type='hidden' name='patient_id' value='${patient_id}'>";
				reser2+="<input type='hidden' name='doctor_id' value='"+doctor_id+"'>";
				reser2+="<input type='hidden' name='jupsoo_date' value='"+thisday+"'>";
				reser2+="<input type='hidden' name='jupsoo_time' value='"+res_time+"'>";
				reser2+="<input type='hidden' name='is_night' value='"+is_night+"'>";
				reser2+="<input type='hidden' name='is_holiday' value='"+is_holiday+"'>";
				reser2+="<input type='hidden' name='jupsoo_code_id' value='"+jupsoo_code_id+"'>";
				reser2+="<input type='hidden' name='schedule_id' value='"+id+"'></form>";
				$("#check_reser").append(reser2);
				$("#addsubmit").submit();
			}
		 function fn_selectChange(){

				var dept_id = $(this).val();
				var dept_name = $("#select_D option:selected").text();
				$.ajax({
					url: 'selectdoctor.do',
					data: 'dept_id='+dept_id,
					dataType : "json",
					success: function(data) {
						var opt = "<option selected>- 담당의사 선택 -</option>";
						$("#sel_doc").html(opt);
						for (i = 0; i < data.doctorlist.length; i++) {
							//alert(data.doctorlist[i].doctor_id);
							var opt2 = "<option value='"+data.doctorlist[i].doctor_id+"'>"+data.doctorlist[i].name+"</option>";
							//alert(opt);
							$("#sel_doc").append(opt2);
						}
				  	}
				});
				
			}
			
	
		</script>
</head>
<body>
	<div id="systembase">

		<!-- 환자 정보 시작 -->
		<div id="systempatient">
			<h3 id="systemmenufont">환자정보</h3>

			<table class="patiend_info_table">
				<tr>
					<td>고유번호</td>
					<td>이름</td>
					<td>성별</td>
					<td>주민번호</td>
					<td>국민보험</td>
					<td>자동차보험</td>
					<td>산업재해보험</td>
					<td>우편번호</td>
					<td>우편번호2</td>
					<td>기본주소</td>
					<td>상세주소</td>
					<td>연락처</td>
				</tr>
				<tr>
					<td>${patient.patient_id }</td>
					<td>${patient.name }</td>
					<td>${patient.gender }</td>
					<td>${patient.jumin1 }-${patient.jumin2}</td>
					<td>${patient.is_kuk }</td>
					<td>${patient.is_car }</td>
					<td>${patient.is_san }</td>
					<td>${patient.zipcode1 }</td>
					<td>${patient.zipcode2 }</td>
					<td>${patient.addr1 }</td>
					<td>${patient.addr2 }</td>
					<td>${patient.tel }</td>
				</tr>
			</table>
		</div>
		<!-- 환자 정보 끝 -->

		<!-- 예약 진료과, 담당의사, 날짜 -->
		<div class="jup_content">
	<h3 id="systemmenufont">환자 예약접수</h3>
			<!-- 진료과 선택 -->
			<table class="jup_table">
			<tr>
			<td>
			<select id="select_D">
					<option selected>-진료과 선택 -</option>
					<c:set var="deptlist2" value="${deptlist }" />
					<c:forEach var="dept" items="${deptlist2}">
						<option  id="select_D2" value="${dept.dept_id }">${dept.name}</option>
					</c:forEach>
				</select>
			</td>
			
			
			<td>
			
			<!-- 담당의사 -->
				<select id="sel_doc">
					<option selected>- 담당의사 선택 -</option>
				</select>
			</td>
			
			
			<td>
			
			<!-- 보험가입 유무 -->
			<select id="bohum" name="bohum">
					<option selected>- 보험 유형 선택 -</option>
					<option value="BO2">국민보험</option>
					<option value="BO3">자동차보험</option>
					<option value="BO4">상해보험</option>
				</select>
			</td>
			<td>
			<select id="is_night" name="is_night">
					<option selected>- 야간여부 -</option>
					<option value="N">일반</option>
					<option value="Y">야간</option>
				</select>
			</td>
			<td>
				<select id="is_holiday" name="is_holiday">
					<option selected>- 공휴일여부 -</option>
					<option value="N">일반</option>
					<option value="Y">공휴일</option>
				</select>
			</td>
			<td>
				<select id="jupsoo_code_id" name="jupsoo_code_id">
					<option selected>- 접수여부 -</option>
					<option value="jcode01">외래</option>
					<option value="jcode02">입원</option>
				</select>
			</td>
			</tr>
			</table>
				

			
			
			
			
			
			<!-- 달력 -->
			<div class="reservation3Div">
				<div class="datepicker" id="datepicker"></div>

			</div>
		
		
			
		</div>





	</div>






	<!-- 오른쪽 환자 대기 리스트 시작 -->
	<div id="systembaseside">
		<h2 style="margin-left: 30px;">예약가능리스트</h2>
		<hr>
		<div class="list_table">
		<input type="text" id="currentDate" placeholder="선택한 날짜" readonly >
		<table id="sidetable"> 
			<tr>
				<th>ID</th>
				<th>시간</th>
				<th>선택</th>
			</tr>

		</table>
		</div>
		<div id="check_reser">
			<div id="check2">
			
			</div>
		</div>
	</div>
	<!-- 오른쪽 환자 대기 리스트 끝 -->

</body>
</html>